// Map system
export const TILE_TYPES = {
    GRASS: 0,
    DIRT: 1,
    WATER: 2,
    STONE: 3,
    TREE: 4,
    WALL: 5
};

export class GameMap {
    constructor(width, height) {
        this.width = width;
        this.height = height;
        this.tiles = [];
        this.collisionMap = [];
        this.spawnPoints = [];
        this.transitions = []; // For scene transitions
    }

    generate() {
        // Generate base terrain
        for (let y = 0; y < this.height; y++) {
            this.tiles[y] = [];
            this.collisionMap[y] = [];
            for (let x = 0; x < this.width; x++) {
                // Basic terrain generation using Perlin noise would go here
                this.tiles[y][x] = TILE_TYPES.GRASS;
                this.collisionMap[y][x] = false;
            }
        }

        // Add features (trees, rocks, etc)
        this.addFeatures();

        // Add monster spawn points
        this.addSpawnPoints();

        // Add transition points
        this.addTransitions();
    }

    addFeatures() {
        // Add trees
        for (let i = 0; i < this.width * this.height * 0.1; i++) {
            const x = Math.floor(Math.random() * this.width);
            const y = Math.floor(Math.random() * this.height);
            if (this.tiles[y][x] === TILE_TYPES.GRASS) {
                this.tiles[y][x] = TILE_TYPES.TREE;
                this.collisionMap[y][x] = true;
            }
        }

        // Add rocks
        for (let i = 0; i < this.width * this.height * 0.05; i++) {
            const x = Math.floor(Math.random() * this.width);
            const y = Math.floor(Math.random() * this.height);
            if (this.tiles[y][x] === TILE_TYPES.GRASS) {
                this.tiles[y][x] = TILE_TYPES.STONE;
                this.collisionMap[y][x] = true;
            }
        }
    }

    addSpawnPoints() {
        // Add monster spawn points
        const spawnCount = Math.floor(this.width * this.height * 0.02);
        for (let i = 0; i < spawnCount; i++) {
            const x = Math.floor(Math.random() * this.width);
            const y = Math.floor(Math.random() * this.height);
            if (!this.collisionMap[y][x]) {
                this.spawnPoints.push({ x, y });
            }
        }
    }

    addTransitions() {
        // Add transition points to other maps
        const transitionPoints = [
            { x: 0, y: Math.floor(this.height / 2), target: 'west_map' },
            { x: this.width - 1, y: Math.floor(this.height / 2), target: 'east_map' },
            { x: Math.floor(this.width / 2), y: 0, target: 'north_map' },
            { x: Math.floor(this.width / 2), y: this.height - 1, target: 'south_map' }
        ];

        this.transitions = transitionPoints;
    }

    isWalkable(x, y) {
        if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
            return false;
        }
        return !this.collisionMap[y][x];
    }

    getSpawnPoint() {
        if (this.spawnPoints.length === 0) return null;
        const index = Math.floor(Math.random() * this.spawnPoints.length);
        return this.spawnPoints[index];
    }

    checkTransition(x, y) {
        return this.transitions.find(t => 
            Math.abs(t.x - x) < 1 && Math.abs(t.y - y) < 1
        );
    }

    draw(ctx, camera) {
        const tileSize = 32;
        const startX = Math.floor(camera.x / tileSize);
        const startY = Math.floor(camera.y / tileSize);
        const endX = startX + Math.ceil(ctx.canvas.width / tileSize) + 1;
        const endY = startY + Math.ceil(ctx.canvas.height / tileSize) + 1;

        for (let y = startY; y < endY; y++) {
            for (let x = startX; x < endX; x++) {
                if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
                    const tile = this.tiles[y][x];
                    const screenX = x * tileSize - camera.x;
                    const screenY = y * tileSize - camera.y;

                    // Draw tile based on type
                    ctx.fillStyle = this.getTileColor(tile);
                    ctx.fillRect(screenX, screenY, tileSize, tileSize);
                }
            }
        }
    }

    getTileColor(tileType) {
        switch (tileType) {
            case TILE_TYPES.GRASS: return '#4CAF50';
            case TILE_TYPES.DIRT: return '#795548';
            case TILE_TYPES.WATER: return '#2196F3';
            case TILE_TYPES.STONE: return '#9E9E9E';
            case TILE_TYPES.TREE: return '#2E7D32';
            case TILE_TYPES.WALL: return '#607D8B';
            default: return '#000000';
        }
    }
}

